package com.hnucm.cooklife.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.hnucm.cooklife.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class EditUsageAdapter extends RecyclerView.Adapter<EditUsageAdapter.EditUsageHolder> {

    private Context mContext;
    private List<Integer> mIntegerList = new ArrayList<>();
    private ItemTouchHelper mItemTouchHelper;

    //自定义接口
    private RecomAdapter.onItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(RecomAdapter.onItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public EditUsageAdapter(Context context, List<Integer> integerList, ItemTouchHelper itemTouchHelper) {
        mContext = context;
        mIntegerList.addAll(integerList);
        mItemTouchHelper = itemTouchHelper;
    }

    public void setIntegerList(List<Integer> integerList) {
        mIntegerList.clear();
        mIntegerList.addAll(integerList);
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public EditUsageHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext)
                .inflate(R.layout.item_edit_origin, parent, false);
        return new EditUsageHolder(view,mOnItemClickListener);
    }

    @Override
    public void onBindViewHolder(@NonNull EditUsageHolder holder, int position) {
        //删除步骤
//        holder.mDeleteUsage.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mIntegerList.remove(holder.getBindingAdapterPosition());
//                notifyItemRemoved(holder.getBindingAdapterPosition());
//                notifyDataSetChanged();
//            }
//        });

        //调整步骤
        holder.mAdjustUsage.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                mItemTouchHelper.startDrag(holder);
                return false;
            }
        });
    }


    @Override
    public int getItemCount() {
        return mIntegerList.size();
    }

    class EditUsageHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.ori_name)
        EditText mOriName;

        @BindView(R.id.ori_much)
        EditText mOriMuch;

        @BindView(R.id.delete_usage)
        ImageView mDeleteUsage;

        @BindView(R.id.adjust_usage)
        ImageView mAdjustUsage;

        public EditUsageHolder(@NonNull View itemView, final RecomAdapter.onItemClickListener onItemClickListener) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            mDeleteUsage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int adapterPosition = getAdapterPosition();
                    if(adapterPosition != RecyclerView.NO_POSITION){
                        onItemClickListener.onSeleClickListener(mDeleteUsage,adapterPosition);
                    }
                }
            });
        }
    }
}
